<template>
  <div class="ai-role-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <div class="header-content">
        <div class="header-left">
          <div class="header-icon">
            <Icon icon="ep:user" />
          </div>
          <div class="header-info">
            <h1 class="page-title">AI角色设定</h1>
            <p class="page-subtitle">配置AI机器人的角色信息和对话规则</p>
          </div>
        </div>
        <div class="header-right">
          <el-button type="primary" :loading="loading" @click="submit" class="save-btn">
            <Icon icon="ep:check" class="btn-icon" />
            {{ loading ? '保存中...' : '保存' }}
          </el-button>
        </div>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <div class="content-wrapper">
        <!-- 角色设定卡片 -->
        <div class="role-card">
          <div class="card-header">
            <div class="card-title">
              <Icon icon="ep:avatar" class="title-icon" />
              <span>角色设定</span>
            </div>
          </div>
          <div class="card-content">
            <div class="role-description">
              <Icon icon="ep:chat-dot-round" class="desc-icon" />
              <span>你是一个电话外呼的专业销售经理</span>
            </div>
          </div>
        </div>

        <!-- 基本信息卡片 -->
        <div class="info-card">
          <div class="card-header">
            <div class="card-title">
              <Icon icon="ep:info-filled" class="title-icon" />
              <span>基本信息</span>
              <span class="required-badge">必填</span>
            </div>
          </div>
          <div class="card-content">
            <div class="form-grid">
              <div class="form-item">
                <label class="form-label">
                  <Icon icon="ep:office-building" class="label-icon" />
                  公司名称
                  <span class="required">*</span>
                </label>
                <el-input
                  v-model="aiRole.companyName"
                  placeholder="石家庄晟宏咨询有限公司"
                  class="enhanced-input"
                  clearable
                />
              </div>

              <div class="form-item">
                <label class="form-label">
                  <Icon icon="ep:user-filled" class="label-icon" />
                  你的名字
                  <span class="required">*</span>
                </label>
                <el-input
                  v-model="aiRole.yourName"
                  placeholder="小李"
                  class="enhanced-input"
                  clearable
                />
              </div>
            </div>
          </div>
        </div>

        <!-- 产品信息卡片 -->
        <div class="info-card">
          <div class="card-header">
            <div class="card-title">
              <Icon icon="ep:goods" class="title-icon" />
              <span>产品信息</span>
              <span class="required-badge">必填</span>
            </div>
          </div>
          <div class="card-content">
            <div class="form-grid">
              <div class="form-item">
                <label class="form-label">
                  <Icon icon="ep:box" class="label-icon" />
                  核心产品
                  <span class="required">*</span>
                </label>
                <el-input
                  v-model="aiRole.coreProduct"
                  placeholder="银行贷款产品"
                  class="enhanced-input"
                  clearable
                />
              </div>

              <div class="form-item">
                <label class="form-label">
                  <Icon icon="ep:star-filled" class="label-icon" />
                  核心卖点
                  <span class="required">*</span>
                </label>
                <el-input
                  v-model="aiRole.coreSellingPoint"
                  placeholder="利息低、放款快、额度高"
                  class="enhanced-input"
                  clearable
                />
              </div>

              <div class="form-item full-width">
                <label class="form-label">
                  <Icon icon="ep:aim" class="label-icon" />
                  核心目的
                  <span class="required">*</span>
                </label>
                <el-input
                  v-model="aiRole.corePurpose"
                  placeholder="邀约客户到公司"
                  class="enhanced-input"
                  clearable
                />
              </div>
            </div>
          </div>
        </div>

        <!-- 对话示例卡片 -->
        <div class="info-card">
          <div class="card-header">
            <div class="card-title">
              <Icon icon="ep:chat-line-round" class="title-icon" />
              <span>对话示例</span>
              <span class="required-badge">必填</span>
            </div>
            <div class="card-tip">
              <Icon icon="ep:info-filled" class="tip-icon" />
              <span>提供真实的对话示例，帮助AI更好地理解对话场景</span>
            </div>
          </div>
          <div class="card-content">
            <div class="textarea-wrapper">
              <el-input
                type="textarea"
                :rows="4"
                v-model="aiRole.example"
                placeholder="销售人员说了:喂，你好...客户说:你们这边是咋办的呀?"
                class="enhanced-textarea"
                show-word-limit
              />
            </div>
          </div>
        </div>

        <!-- 回复限制卡片 -->
        <div class="info-card">
          <div class="card-header">
            <div class="card-title">
              <Icon icon="ep:warning-filled" class="title-icon" />
              <span>回复限制</span>
              <span class="required-badge">必填</span>
            </div>
            <div class="card-tip">
              <Icon icon="ep:info-filled" class="tip-icon" />
              <span>设定AI回复的规则和限制，确保对话质量</span>
            </div>
          </div>
          <div class="card-content">
            <div class="textarea-wrapper">
              <el-input
                type="textarea"
                :rows="4"
                v-model="aiRole.closeRestraint"
                placeholder="回复内容必须简短精炼(不超过20字)..."
                class="enhanced-textarea"
                show-word-limit
              />
            </div>
          </div>
        </div>

        <!-- 额外需求卡片 -->
        <div class="info-card">
          <div class="card-header">
            <div class="card-title">
              <Icon icon="ep:plus" class="title-icon" />
              <span>额外需求</span>
              <span class="optional-badge">可选</span>
            </div>
            <div class="card-tip">
              <Icon icon="ep:info-filled" class="tip-icon" />
              <span>补充其他特殊要求或个性化设置</span>
            </div>
          </div>
          <div class="card-content">
            <div class="textarea-wrapper">
              <el-input
                type="textarea"
                :rows="3"
                v-model="aiRole.specialRestrictions"
                placeholder="请输入额外需求"
                class="enhanced-textarea"
                show-word-limit
                clearable
              />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { getRobotRole, createRobotRole, updateRobotRole } from '@/api/call/ivr/index.ts'

defineOptions({ name: 'AiRole' })

const props = defineProps({
  ivrId: String,
  groupId: String
})

const aiRole = reactive({
  companyName: '',
  yourName: '',
  coreProduct: '',
  coreSellingPoint: '',
  corePurpose: '',
  example: '',
  closeRestraint: '',
  specialRestrictions: ''
})

const aiRoleId = ref(null)
const loading = ref(false)

// 获取详情
const getDetail = async () => {
  try {
    const data = await getRobotRole({ flowId: props.ivrId })
    if (data) {
      aiRoleId.value = data.id || null
      Object.assign(aiRole, { ...data })
    } else {
      aiRoleId.value = null
    }
  } catch (error) {
    console.error('获取AI角色信息失败:', error)
  }
}

// 监听props变化
watch(
  () => props.ivrId,
  (val) => val && getDetail(),
  { immediate: true, deep: true }
)

// 表单验证
const validateForm = () => {
  const requiredFields = [
    { field: 'companyName', name: '公司名称' },
    { field: 'yourName', name: '你的名字' },
    { field: 'coreProduct', name: '核心产品' },
    { field: 'coreSellingPoint', name: '核心卖点' },
    { field: 'corePurpose', name: '核心目的' },
    { field: 'example', name: '对话示例' },
    { field: 'closeRestraint', name: '回复限制' }
  ]

  for (const { field, name } of requiredFields) {
    if (!aiRole[field] || !aiRole[field].trim()) {
      ElMessage.warning(`请填写${name}`)
      return false
    }
  }
  return true
}

// 提交保存
const submit = async () => {
  if (!validateForm()) {
    return
  }

  try {
    loading.value = true
    const obj = { ...aiRole, ivrFlowId: props.ivrId }

    if (aiRoleId.value) {
      await updateRobotRole({ ...obj, id: aiRoleId.value })
    } else {
      await createRobotRole(obj)
    }

    ElMessage.success('保存成功')
    // 重新获取数据以更新ID
    await getDetail()
  } catch (error) {
    console.error('保存失败:', error)
    ElMessage.error('保存失败，请重试')
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.ai-role-container {
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  display: flex;
  height: calc(100% - 40px);
  flex-direction: column;
}

/* 页面头部 */
.page-header {
  background: linear-gradient(135deg, #97a5e2 0%, #7db5db 100%);
  border-radius: 16px;
  padding: 12px 24px;
  margin-bottom: 10px;
  color: white;
  position: relative;
  overflow: hidden;
  height: 40px;
  flex-shrink: 0;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 150px;
    height: 150px;
    background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 0%, transparent 70%);
    border-radius: 50%;
  }
}

.header-content {
  position: relative;
  z-index: 1;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.header-icon {
  width: 48px;
  height: 48px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  backdrop-filter: blur(10px);
}

.page-title {
  font-size: 24px;
  font-weight: 600;
  margin: 0 0 4px 0;
}

.page-subtitle {
  font-size: 14px;
  margin: 0;
  opacity: 0.9;
}

.save-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 2px solid rgba(255, 255, 255, 0.3);
  color: white;
  border-radius: 8px;
  backdrop-filter: blur(10px);

  &:hover {
    background: rgba(255, 255, 255, 0.3);
    border-color: rgba(255, 255, 255, 0.5);
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 主要内容 */
.main-content {
  height: calc(100% - 88px);
  overflow-y: auto;
  /* 美化滚动条样式 */
  &::-webkit-scrollbar {
    width: 6px;
  }

  &::-webkit-scrollbar-track {
    background: #f1f3f4;
    border-radius: 3px;
  }

  &::-webkit-scrollbar-thumb {
    background: linear-gradient(180deg, #c1c9d2 0%, #a8b4c0 100%);
    border-radius: 3px;
    transition: all 0.2s ease;
  }

  &::-webkit-scrollbar-thumb:hover {
    background: linear-gradient(180deg, #0052cc 0%, #1d4ed8 100%);
    box-shadow: 0 2px 4px rgba(0, 82, 204, 0.3);
  }
}

.content-wrapper {
  max-width: 900px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

/* 卡片样式 */
.role-card,
.info-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  backdrop-filter: blur(10px);
  overflow: hidden;
  transition: all 0.2s ease;

  &:hover {
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
  }
}

.card-header {
  padding: 20px 24px 16px;
  border-bottom: 1px solid rgba(0, 82, 204, 0.1);
  background: linear-gradient(135deg, rgba(0, 82, 204, 0.02) 0%, transparent 100%);
}

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;
  color: #1f2937;
  margin-bottom: 8px;
}

.title-icon {
  color: #0052cc;
  font-size: 18px;
}

.required-badge {
  background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%);
  color: white;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 500;
  margin-left: auto;
}

.optional-badge {
  background: linear-gradient(135deg, #6b7280 0%, #4b5563 100%);
  color: white;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 500;
  margin-left: auto;
}

.card-tip {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #6b7280;
}

.tip-icon {
  color: #0052cc;
  font-size: 14px;
}

.card-content {
  padding: 24px;
}

/* 角色描述 */
.role-description {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 20px;
  background: linear-gradient(135deg, rgba(0, 82, 204, 0.05) 0%, rgba(0, 82, 204, 0.02) 100%);
  border-radius: 12px;
  border: 1px solid rgba(0, 82, 204, 0.1);
}

.desc-icon {
  color: #0052cc;
  font-size: 20px;
}

/* 表单样式 */
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 8px;

  &.full-width {
    grid-column: 1 / -1;
  }
}

.form-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 500;
  color: #374151;
}

.label-icon {
  color: #6b7280;
  font-size: 14px;
}

.required {
  color: #ef4444;
  font-weight: 600;
}

.enhanced-input {
  :deep(.el-input__wrapper) {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

.textarea-wrapper {
  position: relative;
}

.enhanced-textarea {
  :deep(.el-textarea__inner) {
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;
    line-height: 1.6;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &:focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }
}

/* 响应式设计 - H5兼容 */
@media (max-width: 768px) {
  .ai-role-container {
    padding: 12px;
  }

  .page-header {
    padding: 20px;
    margin-bottom: 10px;
  }

  .header-content {
    flex-direction: column;
    gap: 16px;
    text-align: center;
  }

  .content-wrapper {
    gap: 16px;
  }

  .card-header {
    padding: 16px 20px 12px;
  }

  .card-content {
    padding: 20px;
  }

  .form-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }

  .form-item.full-width {
    grid-column: 1;
  }

  .save-btn {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .ai-role-container {
    padding: 8px;
  }

  .page-header {
    padding: 16px;
  }

  .page-title {
    font-size: 20px;
  }

  .card-header {
    padding: 12px 16px 8px;
  }

  .card-content {
    padding: 16px;
  }

  .card-title {
    font-size: 14px;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .ai-role-container {
    background: linear-gradient(135deg, #1f2937 0%, #111827 100%);
  }

  .role-card,
  .info-card {
    background: rgba(30, 41, 59, 0.95);
  }

  .page-title {
    color: #f9fafb;
  }

  .card-title {
    color: #f9fafb;
  }

  .form-label {
    color: #e5e7eb;
  }

  .role-description {
    background: linear-gradient(135deg, rgba(0, 82, 204, 0.1) 0%, rgba(0, 82, 204, 0.05) 100%);
  }
}
</style>
